package com.rtx.model.po.service.impl;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.api.annotation.DataSourceAnnotation;
import com.rtx.model.dto.response.EdiResponse;
import com.rtx.model.po.entity.system.User;
import com.rtx.model.po.mapper.UserMapper;
import com.rtx.model.po.service.UserDAO;

@Component
public class UserDAOImpl extends ServiceImpl<UserMapper, User> implements UserDAO{

	@DataSourceAnnotation
	@Override
	public User queryUserByUsername(String datasourcekey, String username) {
		LambdaQueryWrapper<User> qw = Wrappers.lambdaQuery();
		qw.eq(User::getStatus, "1");
		qw.eq(User::getUsername, username);
		return baseMapper.selectOne(qw);
	}

	@DataSourceAnnotation
	@Override
	public void updateLastLoginDate(String datasourcekey, User user) {
		LambdaUpdateWrapper<User> upd = Wrappers.lambdaUpdate();
		upd.eq(User::getId, user.getId());
		upd.set(User::getLastlogintime, user.getLastlogintime());
		baseMapper.update(null, upd);
	}

	@DataSourceAnnotation
	@Override
	public void selectPage(String datasourcekey, Page<User> page, LambdaQueryWrapper<User> qw) {
		baseMapper.selectPage(page, qw);
	}

	@DataSourceAnnotation
	@Override
	public List<User> selectUserByQueryCondition(String datasourcekey, LambdaQueryWrapper<User> qw) {
		return baseMapper.selectList(qw);
	}

	@DataSourceAnnotation
	@Override
	public int insert(String datasourcekey, User user) {
		return baseMapper.insert(user);
	}

	@DataSourceAnnotation
	@Override
	public EdiResponse deleteByusername(String datasourcekey, String username) {
		LambdaQueryWrapper<User> qw = Wrappers.lambdaQuery();
		qw.eq(User::getUsername, username);
		baseMapper.delete(qw);
		return EdiResponse.success();
	}

	@DataSourceAnnotation
	@Override
	public int updateMenuPermission(String datasourcekey, User user) {
		return baseMapper.updateById(user);
	}

	@DataSourceAnnotation
	@Override
	public int deleteByCondition(String datasourcekey, LambdaQueryWrapper<User> qw) {
		return baseMapper.delete(qw);
	}

	@DataSourceAnnotation
	@Override
	public boolean isExistBy(String datasourcekey, LambdaQueryWrapper<User> qw) {
		return baseMapper.exists(qw);
	}

	@DataSourceAnnotation
	@Override
	public void updateByCondition(String datasourcekey,User user) {
		LambdaUpdateWrapper<User> upd = Wrappers.lambdaUpdate();
		upd.eq(User::getId, user.getId());
		if(user.getLastlogintime() != null) {
			upd.set(User::getLastlogintime, user.getLastlogintime());
		}
		if(StringUtils.hasLength(user.getEmail())) {
			upd.set(User::getEmail, user.getEmail());
		}
		if(StringUtils.hasLength(user.getFullname())) {
			upd.set(User::getFullname, user.getFullname());
		}
		if(user.getStatus() != null) {
			upd.set(User::getStatus, user.getStatus());
		}
		if(StringUtils.hasLength(user.getType())) {
			upd.set(User::getType, user.getType());
		}
		baseMapper.update(null, upd);
	}
}
